package com.tezign.intelligence.mini.dam.aifamilyconsultant.util;

import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.github.cdimascio.dotenv.Dotenv;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;

import static com.tezign.intelligence.mini.dam.aifamilyconsultant.constant.Constant.DASHSCOPE_API_KEY;

// 参考文档 https://help.aliyun.com/zh/model-studio/developer-reference/quick-start-7?spm=a2c4g.11186623.help-menu-2400256.d_3_3_8_0.10613d47Ly9x7Z&scm=20140722.H_2712532._.OR_help-V_1
@Component
@Slf4j
public class AsrUtil {


	public static void test() throws Exception {


		Dotenv dotenv = Dotenv.load();
		String dashscopeApiKey = dotenv.get(DASHSCOPE_API_KEY);
		if (dashscopeApiKey == null) {
			throw new NoApiKeyException();
		}
		// 创建Recognition实例
		Recognition recognizer = new Recognition();
		// 创建RecognitionParam，请在实际使用中替换真实apiKey
		RecognitionParam param =
				RecognitionParam.builder()
						.model("paraformer-realtime-v2")
						.format("wav")
						.sampleRate(48000)
						.apiKey(dashscopeApiKey)
						.build();
		// 直接将结果保存到script.txt中
		String result = recognizer.call(param, new File("output.wav"));
		log.info("result={}", result);
	}

	public static void main(String[] args) {
		try {
			test();
			System.exit(0);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

//		// 创建Recognition实例
//		Recognition recognizer = new Recognition();
//		// 创建RecognitionParam，请在实际使用中替换真实apiKey
//		RecognitionParam param =
//				RecognitionParam.builder()
//						.model("paraformer-realtime-v2")
//						.format("wav")
//						.sampleRate(16000)
//						.apiKey(dashscopeApiKey)
//						.build();
//
//		String result = recognizer.call(param, new File("output.wav"));
//		System.out.println(result);
//		return result;


}